package com.cssl.interceptor;

import com.cssl.pojo.User;
import com.cssl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisUtil redisUtil;
    public  MyInterceptor(RedisUtil redisUtil){
        this.redisUtil = redisUtil;
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        System.out.println("===================="+uri);
        if(uri.endsWith("/login")|| uri.endsWith("/yzlogin")|| uri.endsWith("/useronline")|| uri.endsWith("/regist")|| uri.endsWith("/removeuser")){
            return true;
        }
        HttpSession session = request.getSession();
        System.out.println("sid:"+session.getId());
        User user = (User)redisUtil.get(session.getId());
        if (user == null){
            response.sendRedirect("http://localhost/pet/page/login.html");
            return false;
        }
        return true;
    }
}
